package com.dehui.traders.ui.optioanl;



import android.content.Context;
import android.text.TextUtils;

import com.dehui.traders.bean.BaseListResponse;
import com.dehui.traders.bean.OptionalCode;
import com.dehui.traders.bean.OptionalCodeRank;
import com.dehui.traders.bean.RealtimeQuote;
import com.dehui.traders.config.Preferences;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.OptionHelper;
import com.dehui.traders.utils.UserHelp;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.KLog;
import retrofit2.Call;
import retrofit2.Response;

public class MyOptionalStockViewModel extends BaseViewModel{

    private List<OptionalCode> optionalCodes = new ArrayList<>();
    private MyOptiaonalStockFragemnt fragemnt;

    public MyOptionalStockViewModel(Context context, MyOptiaonalStockFragemnt fragemnt) {
        super(context);
        this.fragemnt = fragemnt;
    }

    public BindingCommand addOptionalStockOnClickCommand = new BindingCommand(()->{
        AddOptionalStockActivity.start(fragemnt.getActivity());
    });

    public BindingCommand managerOptionalStockOnClickCommand = new BindingCommand(()->{
        if (UserHelp.getIsLogin(context)) {
            startContainerActivity(OptionalStockManagerFragment.class.getCanonicalName());
        }
    });

    public void getOptionalCodeByPhone() {
        if(TextUtils.isEmpty(Preferences.getToken()))
            return;

        String myOpitonCode = Preferences.getMyOpitonCode();
        try {
            List<OptionalCodeRank> codeList =new Gson().fromJson(myOpitonCode, new TypeToken<List<OptionalCodeRank>>() {}.getType());
            optionalCodes.clear();
            for (OptionalCodeRank item : codeList){
                optionalCodes.add(new OptionalCode(item.code,item.status,item.name,item.market));
            }
            searchRealTime(codeList);
            getCodeByNet();
        }catch (Exception e){
            getCodeByNet();
        }
    }

    private void searchRealTime(List<OptionalCodeRank> list) {
        Call<BaseListResponse<RealtimeQuote>> call = NetImpl.getRealtimeQuote(list);
        call.enqueue(new CallListener<BaseListResponse<RealtimeQuote>>() {
            @Override
            public void _onResponse(Response<BaseListResponse<RealtimeQuote>> response) {
                BaseListResponse<RealtimeQuote> realtimeQuoteBaseListResponse = response.body();
                if (realtimeQuoteBaseListResponse.status == 200){
                    List<RealtimeQuote> realtimeQuoteList = realtimeQuoteBaseListResponse.result;
                    fragemnt.updataList(realtimeQuoteList);
                    OptionHelper.saveRealTimeQuote(context,realtimeQuoteList);
                }
            }

            @Override
            public void _onFailure() {

            }
        });
    }

    private void getCodeByNet() {
        NetImpl.getOptionalByPhone(UserHelp.getUserId())
                .enqueue(new CallListener<BaseListResponse<OptionalCode>>() {
                    @Override
                    public void _onResponse(Response<BaseListResponse<OptionalCode>> response) {
                        BaseListResponse<OptionalCode> optionalCodeBaseListResponse = response.body();
                        int status = optionalCodeBaseListResponse.status;
                        if(status == 200){
                            optionalCodes = optionalCodeBaseListResponse.result;
                            if (optionalCodes == null || optionalCodes.size() <= 0)
                                return;

                            //判断和本地缓存是否相同
                            if (OptionHelper.isCodeSame(optionalCodes)){
                                KLog.i("Hqq","缓存相同");
                                return;
                            }
                            KLog.i("Hqq","缓存不相同");
                            List<OptionalCodeRank> codeRanks = new ArrayList<>();
                            for (int i = 0; i < optionalCodes.size(); i++){
                                OptionalCode optionalCode = optionalCodes.get(i);
                                String code = optionalCode.getCode();
                                OptionalCodeRank codeRank = new OptionalCodeRank(optionalCode.getName(),code,optionalCode.getMarket(),optionalCode.getStatus());
                                codeRanks.add(codeRank);
                            }

                            String codestr = new Gson().toJson(codeRanks);
                            Preferences.saveMyOpitonCode(codestr);

                            searchRealTime(codeRanks);

                        }
                    }

                    @Override
                    public void _onFailure() {

                    }
                });
    }

}
